/*
 * =====================================================================================
 *
 *       Filename:  testBSKDE.cpp
 *
 *    Description:  This is a main file to test the BS_KDE class
 *
 *        Version:  1.0
 *        Created:  07/16/2010 03:54:40 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Jing (gk), race604@gmail.com
 *        Company:  BUPT, Beijing
 *
 * =====================================================================================
 */


#include	<highgui.h>
#include	<cvaux.h>
#include	<cxcore.h>
#include	<iostream>
#include	<string>
#include	<stdlib.h>

#include	"bskde.h"

using namespace std;

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{
	string videoFile;
	CvCapture* capture = NULL;
	IplImage* pFrame;
	IplImage* pFgImg;
	IplImage* pMask;
	CvSize imgSize;

	cvNamedWindow("Video",1);
	cvMoveWindow("Video", 30, 0);

	if (argc > 2)
	{
		fprintf(stderr, "Usage: testbs [video_file_name]");
		return -1;
	}
	
	if (argc == 2)
		videoFile = string(argv[1]);	
	else
		videoFile = "/home/jing/Videos/test_1_v.avi";
	
	if (!(capture = cvCaptureFromFile((char*)videoFile.c_str())))
	{
		cout << "Can't open the video:" << videoFile << endl;
		return -2;
	}

	pMask = cvLoadImage("mask.jpg");
	cvShowImage("mask", pMask);
	pFrame = cvQueryFrame(capture);
	imgSize = {pFrame->width, pFrame->height};
	
	pFgImg = cvCreateImage(imgSize, IPL_DEPTH_8U, 1);
	

	// Background subtract classes
	BS_KDE* bs_kde = new BS_KDE();

	cvNamedWindow("BS_KDE",1);

	while (pFrame = cvQueryFrame(capture))
	{
		cvShowImage("Video", pFrame);

		int time;
		if (time=bs_kde->ProcessFrame(pFrame, pFgImg, pMask))
		{
			cvShowImage("BS_KDE", pFgImg);
			cout << "The KDE time is: " << time << endl;
		}

		int key;
		key = cvWaitKey(100);
		if (key == 'q')
			break;

	}


	return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
